package com.wy.scs.rabbitmq.producer.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DemoProducerReturnCallback {

    @ServiceActivator(inputChannel = "demo-exchange")
    public void handleError(ErrorMessage errorMessage) {
        log.error("处理异常：消息头：{}", errorMessage.getHeaders());
        log.error("处理异常：消息载体：{}", errorMessage.getPayload().getMessage());
        log.error("处理异常：原始消息：{}", errorMessage.getOriginalMessage());
    }

    @StreamListener(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME) // errorChannel
    public void globalHandleError(ErrorMessage errorMessage) {
        log.error("全局异常处理：消息头：{}", errorMessage.getHeaders());
        log.error("全局异常处理：消息载体：{}", errorMessage.getPayload().getMessage());
        log.error("全局异常处理：原始消息：{}", errorMessage.getOriginalMessage());
    }

}
